package com.example.administrator.mydifferutils;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 文 件 名: MyRecyclerViewAdapter
 * 创 建 人: banbury
 * 创建日期: 2017/4/12 0012 14:29
 * 邮   箱:
 * 博   客:
 * 修改时间：
 * 修改备注：
 */

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {


    private List<Bean> list;

    public MyRecyclerViewAdapter(List<Bean> list) {
        this.list = new ArrayList<>(list);
    }

    public void setList(List<Bean> list) {
        this.list = new ArrayList<>(list);
    }

    public List<Bean> getList() {
        return list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_recyclerview, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv_name.setText(list.get(position).getName());
        holder.tv_id.setText(list.get(position).getId() + "");
    }



    @Override
    public void onBindViewHolder(MyViewHolder holder, int position, List<Object> payloads) {
        if (payloads.size() == 0) {
            onBindViewHolder(holder, position);
        } else {
            Bundle bundle = (Bundle) payloads.get(0);
            for (String s : bundle.keySet()) {
                switch (s) {
                    case "name":
                        holder.tv_name.setText(bundle.getString(s));
                        break;
                    case "id":
                        holder.tv_id.setText(bundle.getInt(s) + "");
                        break;
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_id;
        public TextView tv_name;


        public MyViewHolder(View itemView) {
            super(itemView);
            tv_id = (TextView) itemView.findViewById(R.id.tv_id);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
        }
    }
}
